defmodule ProgContest.DockerImages.DockerImage do
  use Ecto.Schema
  import Ecto.Changeset

  alias ProgContest.Quizzes.Quiz

  schema "docker_images" do
    field :image_id, :string
    field :language, :string
    field :code_mounting_point, :string

    belongs_to :quiz, Quiz

    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(docker_image, attrs) do
    docker_image
    |> cast(attrs, [:language, :image_id, :quiz_id, :code_mounting_point])
    |> validate_required([:language, :image_id, :quiz_id, :code_mounting_point])
    |> assoc_constraint(:quiz)
    |> unique_constraint(:language, name: "docker_images_quiz_id_language_index")
  end
end
